import 'package:edu/constants/app_strings.dart';
import 'package:edu/networks/course/models/course_response.dart';
import 'package:edu/utils/app_theme.dart';
import 'package:flutter/material.dart';

class CourseRatingSummaryWidget extends StatelessWidget {
  final CourseReviewSummary summary;

  const CourseRatingSummaryWidget({required this.summary, super.key});

  @override
  Widget build(BuildContext context) {
    if (summary.hasRatings == false) {
      return Text(
        AppStrings.noRatings,
        style: TextStyle(color: AppTheme.shared.greyColor),
      );
    } else {
      return Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _buildTextInfoWidget(context),
            const SizedBox(width: 20),
            _buildRatingDistributeWidget(context),
          ],
        ),
      );
    }
  }

  Widget _buildTextInfoWidget(BuildContext context) {
    return Column(
      children: [
        Text(
          summary.rating.toStringAsFixed(1),
          style: TextStyle(
            fontSize: 60,
            color: Theme.of(context).textTheme.titleMedium?.color,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 0),
        Text(
          AppStrings.fullScoreTip,
          style: TextStyle(fontSize: 20, color: AppTheme.shared.greyColor),
        ),
      ],
    );
  }

  Widget _buildRatingDistributeWidget(BuildContext context) {
    final totalCount = summary.totalCount;
    final star1Count = summary.ratingCounts?[1] ?? 0;
    final star2Count = summary.ratingCounts?[2] ?? 0;
    final star3Count = summary.ratingCounts?[3] ?? 0;
    final star4Count = summary.ratingCounts?[4] ?? 0;
    final star5Count = summary.ratingCounts?[5] ?? 0;

    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          _buildStarProgressRow(context, 5, star5Count, totalCount),
          const SizedBox(height: 1),
          _buildStarProgressRow(context, 4, star4Count, totalCount),
          const SizedBox(height: 1),
          _buildStarProgressRow(context, 3, star3Count, totalCount),
          const SizedBox(height: 1),
          _buildStarProgressRow(context, 2, star2Count, totalCount),
          const SizedBox(height: 1),
          _buildStarProgressRow(context, 1, star1Count, totalCount),
          const SizedBox(height: 10),
          Text(
            '$totalCount ${AppStrings.reviewCountTip}',
            style: Theme.of(context).textTheme.bodySmall?.copyWith(
              color: AppTheme.shared.greyColor,
              fontSize: 15,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStarProgressRow(
    BuildContext context,
    int star,
    int reviewCount,
    int totalCount,
  ) {
    final color = Theme.of(context).textTheme.titleMedium?.color;
    final greyColor = AppTheme.shared.greyColor.withAlpha(128);

    return Row(
      children: [
        SizedBox(
          width: 50,
          child: Row(
            children: [
              const Spacer(),
              ...List.generate(star, (index) {
                return Icon(Icons.star, color: color, size: 10.0);
              }),
            ],
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          flex: reviewCount,
          child: Container(color: color, height: 3),
        ),
        Expanded(
          flex: totalCount - reviewCount,
          child: Container(color: greyColor, height: 3),
        ),
      ],
    );
  }
}
